package com.example.black.myapplication.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.black.myapplication.R;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2019/01/08/10:10
 *     desc   : 画板练习 canvas变换技巧
 *     version: 1.0
 * </pre>
 */

public class MyCanvasView1 extends View {

    private Paint paint;
    private float[] points;
    private float[] points1;
    private Rect rect;
    private RectF rectF;
    private RectF rectF1;
    private RectF rectF2;
    private Path path;
    private float[] radio;
    private Region region;
    private Region region1;
    private Rect emptyRect;
    private RectF rectF3;

    public MyCanvasView1(Context context) {
        super(context);
        initConfig();
    }

    public MyCanvasView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initConfig();
    }

    public MyCanvasView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initConfig();
    }

    private void initConfig() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        points = new float[]{0,0,60,50,160,200,300,300};
        points1 = new float[] {0,0,100,100,200,200,300,300};

        rect = new Rect(0, 0, 100, 200);
        rectF = new RectF(30.4f, 30.4f, 80f, 60f);
        rectF1 = new RectF(40.4f, 50.4f, 80f, 60f);
        rectF2 = new RectF(100, 100, 400, 500);
        rectF3 = new RectF(150, 150, 300, 400);
        path = new Path();
        radio = new float[]{10,10,10,10,10,10,50,60};

        region = new Region(100, 100, 400, 300);
        region1 = new Region();
        emptyRect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //1.平移
        canvas.drawRect(rect,paint);
        canvas.translate(50,50);
        canvas.drawRect(rect,paint);
        canvas.drawRect(rectF,paint);//会一直沿用之前画布设置
        canvas.save();
        //缩放
        paint.setColor(getResources().getColor(R.color.md_cyan_700));
        canvas.drawRect(rectF,paint);
        canvas.scale(1.5f,0.5f);
        canvas.drawRect(rectF,paint);

        canvas.restore();
        canvas.save();
        paint.setColor(getResources().getColor(R.color.md_cyan_A100));
        canvas.drawRect(rectF1,paint);
        canvas.rotate(40);
        canvas.drawRect(rectF1,paint);
        canvas.restore();
        canvas.save();
        canvas.rotate(40,200,300);
        canvas.drawRect(rectF1,paint);
        //斜拉画布
        paint.setColor(getResources().getColor(R.color.md_amber_400));
        canvas.restore();
        canvas.save();
        canvas.drawRect(rectF1,paint);
        //sx,sy倾斜度：X轴方向上倾斜60度，tan60=根号3
        canvas.skew(1.73f,0);
        canvas.drawRect(rectF1,paint);

        //裁剪画布
        paint.setColor(getResources().getColor(R.color.md_amber_700));
        canvas.restore();
        canvas.drawRect(rectF2,paint);
        canvas.clipRect(rectF3);
        canvas.drawColor(getResources().getColor(R.color.md_cyan_A100));
        canvas.drawRect(rectF1,paint);
    }
}
